﻿using System;
using IceCS.DataStructure.Helpers;

namespace IceCS.DataStructure.TrainSequence
{
    /// <summary>
    /// 存储类型
    /// </summary>
    public enum StorageType
    {
        SequenceStack = 1,
        LinkedStack = 2
    }

    class Program
    {
        static void Main(string[] args)
        {
            Carriage[] carriages1 = new Carriage[]
            {
                new Carriage(3),
                new Carriage(6),
                new Carriage(9),
                new Carriage(2),
                new Carriage(4),
                new Carriage(7),
                new Carriage(1),
                new Carriage(8),
                new Carriage(5),
            };
            Carriage[] carriages2 = new Carriage[]
            {
                new Carriage(1),
                new Carriage(2),
                new Carriage(3),
                new Carriage(4),
                new Carriage(5),
                new Carriage(6),
                new Carriage(7),
                new Carriage(8),
                new Carriage(9),
            };
            Carriage[] carriages3 = new Carriage[]
            {
                new Carriage(9),
                new Carriage(8),
                new Carriage(7),
                new Carriage(6),
                new Carriage(5),
                new Carriage(4),
                new Carriage(3),
                new Carriage(2),
                new Carriage(1),
            };
            Carriage[] carriages4 = new Carriage[]
            {
                new Carriage(5),
                new Carriage(4),
                new Carriage(1),
                new Carriage(2),
                new Carriage(6),
                new Carriage(7),
                new Carriage(9),
                new Carriage(8),
                new Carriage(3),
            };
            var type = GetStorageType();
            if (type == StorageType.SequenceStack)
            {
                RunSequenceTrainManager(carriages1);
            }

            if (type == StorageType.LinkedStack)
            {
                RunLinkedTrainManager(carriages1);
            }
        }

        private static void RunSequenceTrainManager(Carriage[] carriages)
        {
            var manager = new SequenceTrainManager(carriages, bufferTrackCount: 3);
            try
            {
                manager.ShowTrainInfo = true;
                manager.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生了错误，错误信息如下：");
                Console.WriteLine(ex);
            }
        }

        private static void RunLinkedTrainManager(Carriage[] carriages)
        {
            var manager = new LinkedTrainManager(carriages, bufferTrackCount: 3);
            try
            {
                manager.ShowTrainInfo = true;
                manager.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生了错误，错误信息如下：");
                Console.WriteLine(ex);
            }
        }

        /// <summary>
        /// 获取存储类型
        /// </summary>
        private static StorageType GetStorageType()
        {
            var tips = "请选择存储结构的类型：\n1.顺序栈\n2.链式栈";
            var conditionList = new string[] { "1", "2" };
            var input = ConsoleHelper.GetRightInputNumber(tips, num => num >= 1 && num <= 2);
            return (StorageType)input;
        }
    }
}
